//
//  ZSHomeListViewModel.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/12.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
import SVProgressHUD
//这是一个home页的viewModel,负责home页的数据的管理

class ZSHomeListViewModel: NSObject {
    
    
    override init() {
        super.init()

//        loadHomeData { (isSuccess, result) -> () in
//            if isSuccess{
//            
//            self.arrayM = result
//        
//            }
//        }
    }
    
    
    //??这里怎么会有一个bug呢??
    
    
    
    
//    lazy var arrayM: [ZSStatus] = {
//    
//        let a = [ZSStatus]()
//        
//        return a
//    }()
//    
//
}




//HomeListViewModel提供的加载首页数据的方法

extension ZSHomeListViewModel{
   
    func loadHomeData(finished:(isSuccess:Bool,result:[ZSStatus])->()){

        
        ZSNetworkTools.sharedTools.loadHomeVCData((ZSUserAccountViewModel.sharedUserViewModel.userAccount?.access_token)!) { (result, error) -> () in
    
            
            if error != nil {
            
            SVProgressHUD.showErrorWithStatus("世界上最蛋疼的就是没网")
                return
            }
    
            guard let resultDic = result!["statuses"] as? [[String:AnyObject]] else{
                print("数据格式错误")
                return
            }
            
            var temp = [ZSStatus]()
            
            for dic in resultDic{
                
                let s = ZSStatus.init(dict: dic)
                
                temp.append(s)
                
            }
            

            finished(isSuccess: true,result: temp)
            
        }
    }

    

    //HomeListViewModel提供的加载更多数据的方法-->返回一个包含较早数据的数组,,把这个返回的数据拼接到tableView的数据数组中,刷新数据进行显示

    func loadMoreHomeData(max_id:Int64,finished:(isSuccess:Bool,result:[ZSStatus])->()){
        
        ZSNetworkTools.sharedTools.loadMoreHomeVCData(max_id, access_token: (ZSUserAccountViewModel.sharedUserViewModel.userAccount?.access_token)!) { (result, error) -> () in
            
            if error != nil {
                
                SVProgressHUD.showErrorWithStatus("世界上最蛋疼的就是没网")
                return
            }
            
            guard let resultDic = result!["statuses"] as? [[String:AnyObject]] else{
                print("数据格式错误")
                return
            }
            
            var temp = [ZSStatus]()
            
            for dic in resultDic{
                
                let s = ZSStatus.init(dict: dic)
                
                temp.append(s)
                
            }
            finished(isSuccess: true,result: temp)
        }
    }
    
    
    
    //HomeListViewModel提供的加载最新数据的方法-->返回一个包含最新数据的数组,,把这个返回的数据替换tableView的数据数组中,刷新数据进行显示
    
    func loadNewHomeData(since_id:Int64,finished:(isSuccess:Bool,result:[ZSStatus])->()){
        
        ZSNetworkTools.sharedTools.loadNewHomeVCData(since_id, access_token: (ZSUserAccountViewModel.sharedUserViewModel.userAccount?.access_token)!) { (result, error) -> () in
            
            if error != nil {
                
                SVProgressHUD.showErrorWithStatus("世界上最蛋疼的就是没网")
                return
            }
            
            guard let resultDic = result!["statuses"] as? [[String:AnyObject]] else{
                print("数据格式错误")
                return
            }
            
            var temp = [ZSStatus]()
            
            for dic in resultDic{
                
                let s = ZSStatus.init(dict: dic)
                
                temp.append(s)
                
            }
            finished(isSuccess: true,result: temp)
        }
    }

    
    
    
    
}
